﻿
#region Copyright & License
// --------------------------
// Copyright 2008 Daniel Gray
// --------------------------
// This file is part of Kale.
//
// Kale is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kale is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Kale.  If not, see <http://www.gnu.org/licenses/>.
#endregion
#region File details
// --------------------------------------------------------
// Filename          : Global.asax.cs
// Author            : Daniel Gray
// Created           : 20/04/2008
// --------------------------------------------------------
#endregion
            
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using Kale.Core;
using Kale.Core.Configuration;
using System.IO;
using log4net;
using log4net.Config;

namespace Kale.Web.GUI
{
   /// <summary>
   /// Provides global application handlers.
   /// </summary>
   public class Global : System.Web.HttpApplication
   {

      private static ILog _logger;

      /// <summary>
      /// Handles the Start event of the Application control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
      protected void Application_Start(object sender, EventArgs e)
      {
         // configure logging
         FileInfo log4netConfigFile = new FileInfo(HttpContext.Current.Server.MapPath("log4net.config"));
         XmlConfigurator.ConfigureAndWatch(log4netConfigFile);

         _logger = LogManager.GetLogger(typeof(Global));

         _logger.Info("--- Application startup ---");
         _logger.Info(string.Format("Logging configured from config file {0}", log4netConfigFile.FullName));

         string cachePath = HttpContext.Current.Server.MapPath(KaleConfigurationSection.Default.ImageCaching.CachePath);

         // configure the image cache
         ImageCache.Configure(cachePath, KaleConfigurationSection.Default.ImageCaching.MaximumCacheSize, KaleConfigurationSection.Default.ImageCaching.ShrinkagePercent);
         _logger.Info(string.Format("ImageCache configured for path : {0}, maximum size : {1}Mb", cachePath, KaleConfigurationSection.Default.ImageCaching.MaximumCacheSize));

         _logger.Info("--- Application startup complete ---");
      }

      /// <summary>
      /// Handles the Start event of the Session control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
      protected void Session_Start(object sender, EventArgs e)
      {

      }

      /// <summary>
      /// Handles the BeginRequest event of the Application control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
      protected void Application_BeginRequest(object sender, EventArgs e)
      {

      }

      /// <summary>
      /// Handles the AuthenticateRequest event of the Application control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
      protected void Application_AuthenticateRequest(object sender, EventArgs e)
      {

      }

      /// <summary>
      /// Handles the Error event of the Application control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
      protected void Application_Error(object sender, EventArgs e)
      {

      }

      /// <summary>
      /// Handles the End event of the Session control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
      protected void Session_End(object sender, EventArgs e)
      {

      }

      /// <summary>
      /// Handles the End event of the Application control.
      /// </summary>
      /// <param name="sender">The source of the event.</param>
      /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
      protected void Application_End(object sender, EventArgs e)
      {

      }
   }
}